package cn.bluarry.downloaddemo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import java.io.File;

public class DownLoadService extends Service {

    private DownLoadTask downLoadTask;
    private String downloadUrl;


    private DownLoadListener downLoadListener=new DownLoadListener() {
        @Override
        public void onProgress(int progress) {
            getNotificationManager().notify(1,getNotification("下载中...",progress));
        }

        @Override
        public void onSuccess() {
            downLoadTask=null;
            stopForeground(true);
            getNotificationManager().notify(1,getNotification("下载完成",-1));
            Toast.makeText(DownLoadService.this,"下载完成",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onFaild() {
            downLoadTask=null;
            Toast.makeText(DownLoadService.this,"下载失败",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPause() {
            downLoadTask=null;
            Toast.makeText(DownLoadService.this,"下载暂停",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCanceled() {
            downLoadTask=null;
            stopForeground(true);
            Toast.makeText(DownLoadService.this,"下载取消",Toast.LENGTH_SHORT).show();

        }
    };


    public DownLoadService() {
    }

    private DownloadBinder mBinder=new DownloadBinder();

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return  mBinder;
    }


    public NotificationManager getNotificationManager(){
        return (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }
    public Notification getNotification(String title,int progress){
        String CHANNEL_ID="cn.bluarry.downloaddemo.n1";
        String CHANNEL_NAME = "TEST";
        NotificationChannel notificationChannel = null;
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
            notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            getNotificationManager().createNotificationChannel(notificationChannel);
        }
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this,CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
            .setContentIntent(pi)
            .setContentTitle(title);
        if(progress>0){
            builder.setContentTitle(progress+"%");
            builder.setProgress(100,progress,false);
        }
        return builder.build();
    }



    class DownloadBinder extends Binder{
        public void StartDownload(String url){
            if(downLoadTask==null){
                downloadUrl=url;
                downLoadTask=new DownLoadTask(downLoadListener);
                downLoadTask.execute(downloadUrl);
                startForeground(1,getNotification("下载中",0));
                Toast.makeText(DownLoadService.this,"下载开始",Toast.LENGTH_SHORT);
            }
        }

        public void PauseDownload(){
            if(downLoadTask!=null){
                downLoadTask.PauseDownload();
            }
        }

        public void cancenDownload(){
            if(downLoadTask!=null){
                downLoadTask.CancelDownload();
            }else{
                String fileName=downloadUrl.substring(downloadUrl.lastIndexOf("/"));
                String directory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
                File file=new File(directory+fileName);
                if(file.exists()){
                    file.delete();
                }
                getNotificationManager().cancel(1);
                stopForeground(true);
                Toast.makeText(DownLoadService.this,"下载取消",Toast.LENGTH_SHORT).show();
            }
        }


    }

}
